Skip to main content

Variables and Naming Conventions

1. Variable Declaration & Assignments

A variable stores data values in memory, which can be referenced and manipulated in the program. Variables in Python are dynamically typed, meaning you don’t need to declare their data type explicitly.

x = 10
y = "Hello"

Python allows you to assign multiple variables in a single line. Just separate the variables with a comma, and the right-hand side values with a comma as well.

Ensure the number of variables matches the number of values you're assigning.

msg1, msg2 = "Hello", "World"

#equivalent to`
msg1 = "Hello"
msg2 = "World"

We can also use multiple assignments to swap the values of variables:

msg1, msg2 = "Hello", "World" # msg1 = "Hello", msg2 = "World"
msg1, msg2 = msg2, msg1 # msg1 = "World", msg2 = "Hello"

2. Variable Reassignment

Variables can change their value, this is called reassigning a variable.

x = 5
x = "Now I'm a string"

Python allows you to reassign different types of values to the same variable, but this can sometimes

3. Variable Naming & Convention

There are some strict rules for naming variables, if you break any of these rules it will cause an error:

  • Variable names can only contain letters, numbers, and underscores.
  • Variable names can't start with a number.
  • Variable names can't contain spaces.
  • Variable names can't be the same as Python keywords like forif, etc.
my_number = 5
my_string = "text"

More than having good variable names, it's important to follow naming conventions. There are several different naming conventions such as:

  • Camel Case: The first letter of each word is capitalized except for the first word. For example, myVariableName.
  • Snake Case: Words are separated by underscores. For example, my_variable_name.
  • Pascal Case: The first letter of each word is capitalized. For example, MyVariableName.

In Python, we typically use the snake case. This means we use underscores (_) to separate words in variable names. lead to confusion. Keep an eye on the types you're working with.

4. Variable Types

Variables in Python can hold different types of data, such as integersdecimal numbers (aka floating-point numbers), stringsbooleanslists, and more. Python will infer the type based on the value assigned.

  • A variable is an integer type when it holds a whole number value.
  • A variable is a floating-point number type when it holds a decimal number value.
  • A variable is a boolean type when it holds a True or False value.
  • A variable is a string type when it holds a sequence of characters, inside quotes.
  • A variable is a list type when it holds a collection of comma-separated values, inside square brackets.
age = 25             # integer
temperature = 98.6 # floating-point number
is_true = True # boolean
name = "Alice" # string
my_list = [1, 2, 3] # list

We can print the type of a variable or value using the type() function in Python. For example:

print(type(10))

This will output:

<class 'int'>

5. Dynamic Typing

In Python, a single variable's type can change throughout the code. This is called dynamic typing. For example, the following code will run without any errors:

variable = 10         # int type
variable = "Hello" # str type
variable = [1, 2, 3] # list type

Not all languages support dynamic typing. In many languages, a variable's type must be explicitly declared and cannot be changed. This is called static typing.

Dynamic typing should generally be avoided when possible. This means you should avoid changing a variable's type throughout your code. If you do, you may not know what type a variable is at any given time, which can cause bugs and make your code harder to understand.

6. Type Casting

A variable in Python can be converted to a different type using type casting. For example, the following code will run without any errors:

variable = 10.9
print(int(variable))

This code will output:

10

The variable variable is a floating-point number. The int() function converts it to an integer. The output is the integer part of the floating-point number, aka rounding the number down.

TypeError

Even though variable types can change, there are still rules about what types of variables can be used together. For example, the following code will cause an error:

message = "Hello"
message = int(message)

We can't convert a string to an integer unless the string is a number. This code will cause a TypeError.

7. Empty Variable

If we want to declare a variable without a value, we can use the None keyword. This is used to indicate that the variable has no value.

If we try to declare a variable without explicitly assigning a value to it, we will get an error.

var

However, if we assign the variable to None, we can declare the variable without a value.

var = None

print(type(var)) # <class 'NoneType'>